cap log close
snapshot erase _all
clear all

global filenum 02
global filename "gen_ASEC_panel"

log using "${log_path}/${filenum}_${filename}.smcl", replace

*===========================*
*	Load In Monthly Data		*
*===========================*
	use "${data_path}/cps_00119.dta" , replace
	
	*keep if year>=2017 & year<=2020

	format cpsidp %18.0f
	format cpsid %18.0f

	keep if asecflag==. | (month==3 & asecflag==2)
	
	drop occ2010
	
	gen asec_year = year  if inrange(month,1,6)
	replace asec_year = year+1 if month==12
	
	keep if inlist(month,12,1,2,3,4,5,6)
	
	keep if inrange(asec_year,2018,2020)
	
	gen datevar = ym(year, month)
	format datevar %tmMon_CCYY
	
	tempfile monthlycps
	save `monthlycps'
	
*===========================*
*	Setup ASEC Panel		*
*===========================*
	
	// keep only asec data for 2018 - 2021
	use "${data_path}/cps_00119.dta" , replace
	keep if year>=2018 & year<=2021

	format cpsidp %18.0f
	format cpsid %18.0f
	
	keep if asecflag==1		// load in ASEC
	drop if asecoverp==1  	/* asec oversample, no march basic info  */
	
	gen firmsize_asec = firmsize
	
	gen inc_asec=inctot
	
	sort cpsidp year
	by cpsidp: gen firmsize_nextyear=firmsize_asec[_n+1]
	
	#delimit ;
			label define sizeLabel 	0 "NA"
									1 "under 10"
									2 "10 to 24"
									4 "10 to 49"
									5 "25 to 99"
									6 "50 to 99"
									7 "100 to 499"
									8 "500 to 999"
									9 "1000 +";
	#delimit cr	
	 

	label values firmsize_asec sizeLabel
	
	label values firmsize_nextyear sizeLabel	
	
	*****************************
	*	Telework Variables		*
	* 	From Misty 				*
	*****************************
	 
		cap drop _merge

		snapshot save
		
		*===========================*
		*	Telwork Variable 		*
		*	Based on occ2010ly		*
		*===========================*
		
			** Requires renaming varible to facilitate merge

			drop occ2010 
			rename occ10ly occ2010
			
			merge m:1 occ2010 using "${data_path}/census2010_tw.dta", nogen keep(1 3)
			
			*There are 48 Occupation codes that are left unassigned, I assign 47 of those manually. After merging the file, use the following code

			replace tw=	1	  if occ2010==		30
			replace tw=	1	  if occ2010==		130
			replace tw=	1	  if occ2010==		510
			replace tw=	1	  if occ2010==		520
			replace tw=	1	  if occ2010==		530
			replace tw=	0	  if occ2010==		560
			replace tw=	1	  if occ2010==		620
			replace tw=	0	  if occ2010==		720
			replace tw=	1	  if occ2010==		730
			replace tw=	1	  if occ2010==		1000
			replace tw=	1	  if occ2010==		1020
			replace tw=	1	  if occ2010==		1050
			replace tw=	1	  if occ2010==		1100
			replace tw=	1	  if occ2010==		1240
			replace tw=	1	  if occ2010==		1960
			replace tw=	1	  if occ2010==		2020
			replace tw=	1	  if occ2010==		2060
			replace tw=	1	  if occ2010==		2140
			replace tw=	1	  if occ2010==		2150
			replace tw=	1	  if occ2010==		2760
			replace tw=	1	  if occ2010==		2810
			replace tw=	0	  if occ2010==		3060
			replace tw=	0	  if occ2010==		3130
			replace tw=	1	  if occ2010==		3240
			replace tw=	0	  if occ2010==		3410
			replace tw=	0	  if occ2010==		3530
			replace tw=	0	  if occ2010==		3600
			replace tw=	0	  if occ2010==		3650
			replace tw=	1	  if occ2010==		3730
			replace tw=	1	  if occ2010==		3950
			replace tw=	0	  if occ2010==		4650
			replace tw=	1	  if occ2010==		4965
			replace tw=	0	  if occ2010==		5030
			replace tw=	1	  if occ2010==		5165
			replace tw=	1	  if occ2010==		5420
			replace tw=	1	  if occ2010==		5940
			replace tw=	0	  if occ2010==		7750
			replace tw=	0	  if occ2010==		7855
			replace tw=	0	  if occ2010==		7900
			replace tw=	0	  if occ2010==		8220
			replace tw=	0	  if occ2010==		8230
			replace tw=	0	  if occ2010==		8460
			replace tw=	0	  if occ2010==		8550
			replace tw=	1	  if occ2010==		9000
			replace tw=	0	  if occ2010==		9100
			replace tw=	0	  if occ2010==		9150
			replace tw=	0	  if occ2010==		9750	

			replace avtw=tw if tw!=. & avtw==.
			
			gen tw_binary=(tw>=0.5)
				replace tw_binary=. if tw==.	
				
			rename tw_binary tw_binary_10ly
			rename tw tw_10ly 
				
			keep cpsidp year tw_10ly tw_binary_10ly
		
			tempfile telework2010ly
			save `telework2010ly'
			
			
		*===========================*
		*	Telwork Variable 		*
		*	Based on occ2010		*
		*===========================*			
			snapshot restore 1	
			
			merge m:1 occ2010 using "${data_path}/census2010_tw.dta", nogen keep(1 3)
			
			*There are 48 Occupation codes that are left unassigned, I assign 47 of those manually. After merging the file, use the following code

			replace tw=	1	  if occ2010==		30
			replace tw=	1	  if occ2010==		130
			replace tw=	1	  if occ2010==		510
			replace tw=	1	  if occ2010==		520
			replace tw=	1	  if occ2010==		530
			replace tw=	0	  if occ2010==		560
			replace tw=	1	  if occ2010==		620
			replace tw=	0	  if occ2010==		720
			replace tw=	1	  if occ2010==		730
			replace tw=	1	  if occ2010==		1000
			replace tw=	1	  if occ2010==		1020
			replace tw=	1	  if occ2010==		1050
			replace tw=	1	  if occ2010==		1100
			replace tw=	1	  if occ2010==		1240
			replace tw=	1	  if occ2010==		1960
			replace tw=	1	  if occ2010==		2020
			replace tw=	1	  if occ2010==		2060
			replace tw=	1	  if occ2010==		2140
			replace tw=	1	  if occ2010==		2150
			replace tw=	1	  if occ2010==		2760
			replace tw=	1	  if occ2010==		2810
			replace tw=	0	  if occ2010==		3060
			replace tw=	0	  if occ2010==		3130
			replace tw=	1	  if occ2010==		3240
			replace tw=	0	  if occ2010==		3410
			replace tw=	0	  if occ2010==		3530
			replace tw=	0	  if occ2010==		3600
			replace tw=	0	  if occ2010==		3650
			replace tw=	1	  if occ2010==		3730
			replace tw=	1	  if occ2010==		3950
			replace tw=	0	  if occ2010==		4650
			replace tw=	1	  if occ2010==		4965
			replace tw=	0	  if occ2010==		5030
			replace tw=	1	  if occ2010==		5165
			replace tw=	1	  if occ2010==		5420
			replace tw=	1	  if occ2010==		5940
			replace tw=	0	  if occ2010==		7750
			replace tw=	0	  if occ2010==		7855
			replace tw=	0	  if occ2010==		7900
			replace tw=	0	  if occ2010==		8220
			replace tw=	0	  if occ2010==		8230
			replace tw=	0	  if occ2010==		8460
			replace tw=	0	  if occ2010==		8550
			replace tw=	1	  if occ2010==		9000
			replace tw=	0	  if occ2010==		9100
			replace tw=	0	  if occ2010==		9150
			replace tw=	0	  if occ2010==		9750	

			replace avtw=tw if tw!=. & avtw==.
			
			gen tw_binary=(tw>=0.5)
				replace tw_binary=. if tw==.	
				
			gen occ2010_asec=occ2010
		
		keep cpsidp year firmsize_asec firmsize_nextyear inc_asec occ2010 tw_binary avtw tw occ2010_asec occ10ly classwly
		drop if year==2021
	
	** Merging on telework variable based on occ10ly as alternative measure
	merge 1:1 cpsidp year using `telework2010ly', keep(1 3) nogen keepusing(tw_binary_10ly tw_10ly)
	
	snapshot erase 1
*===========================*
*	Merge ASEC Panel		*
*===========================*
	rename year asec_year
		
	merge 1:m cpsidp asec_year using `monthlycps', keep(3) nogen		// keep only people in monthly who are also in the ASEC
		
	gen firmsize_new=firmsize_asec if inlist(month,12,1,2,3) 
	replace firmsize_new=firmsize_nextyear if inrange(month,4,6)
	
	label values firmsize_nextyear sizeLabel	

	label values firmsize_new sizeLabel
	gen under500_new=(firmsize_new<8)
		replace under500_new=. if firmsize_new==0
	
	gen under500_asec=(firmsize_asec<8)
		replace under500_asec=. if firmsize_asec==0
	
	** Merge in State Names
	gen state_fips=statefip
	merge m:1 state_fips using "${data_path}/statastates.dta", keep(1 3) keepusing(state_abbrev)
	rename state_abbrev state
	
	/* Select Rotation Groups	*/

	gen temp=floor(cpsidp/100000000)
	tostring temp, replace

	gen startyr=substr(temp,1,4)
		destring startyr, replace
	gen startmo=substr(temp,5,6)
		destring startmo, replace
	drop temp	
	

*===============================*
*	Merge CDC Case Counts		*
*===============================*
	sort statefip 
	
	merge m:1 statefip using "${data_path}/cdc_by_state.dta", keep(1 3) nogen

	sort statefip month year
	merge m:1 statefip month year using "${data_path}/cdc_by_state_by_month.dta", keep(1 3) nogen
	
*===========================*
*	Setup Analysis Vars		*
*===========================*

	drop if age<15
	
	*************************
	*	LFP Variables		*
	*************************
		gen inlf = (empstat<32)
		
		gen unemp = 	(empstat==21 | empstat==22)
			replace unemp=. if inlf==0
			
	*****************************
	*	Absence Variables		*
	*****************************
		
		gen absent_withJob=.
			replace absent_withJob=1 if absent>1						// Yes (laid off or other reason)	
			replace absent_withJob=0 if absent==0 & empstat==10			// at work
			replace absent_withJob=0 if absent==0 & empstat==1			// armed forces
			
		gen absent_yes =(absent==3)
		
			gen absent_vacation =	(whyabsnt==5)
			gen absent_sick = 		(whyabsnt==6)
			gen absent_family = 	(whyabsnt==7 | whyabsnt==8 | whyabsnt==9)
			gen absent_other = 		(whyabsnt>=10)		
			
		 gen absent_paid = (uh_absrea_b2>=1 & uh_absrea_b2<=10) |  (uh_absrea_b2>=21 & uh_absrea_b2<=30) 
		
			gen absent_paid_vacation = 	(uh_absrea_b2==1 | uh_absrea_b2==21) 
			gen absent_paid_sick = 		(uh_absrea_b2==2 | uh_absrea_b2==22) 
			gen absent_paid_family = 	(uh_absrea_b2>=3 & uh_absrea_b2<=5) |  (uh_absrea_b2>=23 & uh_absrea_b2<=25) 
			gen absent_paid_other =		(uh_absrea_b2>=6 & uh_absrea_b2<=10) |  (uh_absrea_b2>=26 & uh_absrea_b2<=30) 

		gen absent_unpaid = (uh_absrea_b2>=11 & uh_absrea_b2<=20) |  (uh_absrea_b2>=31 & uh_absrea_b2<=40) 
		
			gen absent_unpaid_vacation = 	(uh_absrea_b2==11 | uh_absrea_b2==31) 
			gen absent_unpaid_sick = 		(uh_absrea_b2==12 | uh_absrea_b2==32) 
			gen absent_unpaid_family = 		(uh_absrea_b2>=13 & uh_absrea_b2<=15) |  (uh_absrea_b2>=33 & uh_absrea_b2<=35) 
			gen absent_unpaid_other = 		(uh_absrea_b2>=16 & uh_absrea_b2<=20) |  (uh_absrea_b2>=36 & uh_absrea_b2<=40) 	
						
	*****************************
	*	Demog Variables			*
	*****************************
		
		gen female=(sex==2)
		
		gen childUnder18=(nchild>0)
		gen childUnder5=(nchlt5>0)
		
		gen childUnder12=(yngch<=12)
		
		gen someCollege=(educ>=81)
		
		gen bachelors = (educ>=111)
		
		gen married = (marst<=2)
		
		gen raceCat=.
			replace raceCat=1 if race==100 & hispan==0		// white, not hispanic
			replace raceCat=2 if race==200 & hispan==0		// black, not hispanic
			replace raceCat=3 if hispan!=0					// hispanic
			replace raceCat=4 if raceCat==.					// everyone else not hispanic
		
		#delimit ;
			label define raceLabel 	1 "White, Not Hispanic" 
									2 "Black, Not Hispanic" 
									3 "Hispanic"
									4 "Other";
		#delimit cr
		
		label values raceCat raceLabel		

		gen underFPL=0
			replace underFPL=1 if faminc<=470 & famsize==1		// 470: up to 14,999 (12,880)
			replace underFPL=1 if faminc<=500 & famsize==2		// 500: up to 19,999 (17,420)
			replace underFPL=1 if faminc<=600 & famsize==3		// 600: up to 24,999 (21,960)
			replace underFPL=1 if faminc<=710 & famsize==4		// 710: up to 29,999 (26,500)
			replace underFPL=1 if faminc<=720 & famsize==5		// 720: up to 34,999 (31,040)
			replace underFPL=1 if faminc<=730 & famsize==6		// 730: up to 39,999 (35,580)
			replace underFPL=1 if faminc<=740 & famsize==7		// 740: up to 49,999 (40,120)
			replace underFPL=1 if faminc<=740 & famsize>=8		// 740: up to 49,999 (44,660)
		
	
	*****************************
	*	Job Variables			*
	*****************************
	
		gen occCat=.
			replace occCat=1 	if occ>0		& occ<=960
			replace occCat=2 	if occ>960 		& occ<=3550
			replace occCat=3 	if occ>3550 	& occ<=4655
			replace occCat=4 	if occ>4655 	& occ<=4965
			replace occCat=5 	if occ>4965 	& occ<=5940
			replace occCat=6 	if occ>5940 	& occ<=6130
			replace occCat=7 	if occ>6130 	& occ<=6950
			replace occCat=8 	if occ>6950 	& occ<=7640
			replace occCat=9 	if occ>7640 	& occ<=8990
			replace occCat=10	if occ>8990 	& occ<=9760
			replace occCat=11	if occ>9760
		
		#delimit ;
			label define occLabel 	1 "Manangement"
									2 "Professional"
									3 "Service"
									4 "Sales"
									5 "Office"
									6 "Farming"
									7 "Construction"
									8 "Maintenance"
									9 "Production"
									10 "Transportation"
									11 "Armed Forces";
		#delimit cr
		
		label values occCat occLabel				
			
		gen occCat_1=1 if (occ>=2200 & occ<2600)				// teachers and librarians
			replace occCat_1=2 if (occ>3700 & occ<=4650)		// service 
			replace occCat_1=3 if (occ>=3000 & occ<3700)		// healthcare
			replace occCat_1=4 if occCat_1==.
			
		#delimit ;
			label define occLabel_1 1 "Teachers"
									2 "Service"
									3 "Healthcare"
									4 "Other";
		#delimit cr
		
		label values occCat_1 occLabel_1				
		
	*****************************
	*	Leave Variables			*
	*****************************
		
		gen state_sick_leave=(	state=="CT" | state=="DC" | state=="CA" | state=="MA" ///
							  | state=="OR" | state=="VT" | state=="AZ" | state=="WA" ///
							  | state=="MD" | state=="RI" | state=="NJ" | state=="MI" )	// state-manded sick leave

		gen some_sick_leave=state_sick_leave	
			replace some_sick_leave=1 if state=="PA" | state=="IL" | state=="NY"
		
		gen no_sick_leave=(some_sick_leave==0)
	
	*********************************
	*	Public Private Distinction	*
	*********************************	
	
	gen private=.
	replace private = 1 if classwly>=13 & classwly<=23
	replace private = 0 if classwly>=25 & classwly<=28
	** niu and unpaid family workers not defined 

	drop if private==. 	// unpaid family workers
	
	
	
	*****************************
	*	Drop Armed Forces		*
	*****************************
		drop if empstat==1		
		
save "${data_path}/asec_2018_2020.dta" , replace 
